package com.uenpay.util.common;

import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;

public class HardWareUtils {

    public static String deviceInfo = null;

    /**
     * 网络是否可用
     */
    public static boolean networkIsAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        connectivity = null;
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 获取手机系统SDK版本
     *
     * @return 如API 17 则返回 17
     */
    public static int getSDKVersion() {
        return Build.VERSION.SDK_INT;
    }

    /**
     * 获取设备的可用内存大小
     *
     * @param context 应用上下文对象context
     * @return 当前内存大小
     */
    public static int getDeviceUsableMemory(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        MemoryInfo mi = new MemoryInfo();
        am.getMemoryInfo(mi);
        // 返回当前系统的可用内存
        return (int) (mi.availMem / (1024 * 1024));
    }

    /**
     * 获取机器设备信息
     *
     * @return 手机品牌+手机信号+sdk+应用版本号
     */
    public static String getDeviceInfo() {
        if (deviceInfo == null) {
            deviceInfo = "Android_" + Build.BRAND + "_" + Build.MODEL + "_" + getSDKVersion() + "_v" + AppConfig.VERSION_INFO.getVersionName();
        }
        return deviceInfo;
    }

}
